﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using Microsoft.Practices.Web.UI.WebControls;
using TaskKernel.Domain;
using TaskWebApplicationUI.PresenterLayer;

namespace TaskWebApplicationUI
{
    public interface IAdminPermissionsView : IAbstractView
    {
        Guid DepartmentId { get; }
        List<DepartmentUser> UsersWithRoles { set; }
        List<Account> Accounts { set; }
        string Message { set; }
    }

    public partial class AdminPermissions : ViewPage<IAdminPermissionsView, AdminPermissionsPresenter>,
                                            IAdminPermissionsView
    {
        #region IAdminPermissionsView Members

        public string DepartmentName
        {
            set { Department.Text = value; }
        }

        public Guid DepartmentId
        {
            get { return new Guid(Request.QueryString["ID"]); }
        }

        public List<DepartmentUser> UsersWithRoles
        {
            set
            {
                DepartmentUser element = value.First();
                ObjectContainerDataSource1.DataObjectTypeName = element.GetType().AssemblyQualifiedName;
                ObjectContainerDataSource1.DataSource = value;
            }
        }

        public List<Account> Accounts
        {
            set
            {
                AccountDDL.DataSource = value;
                AccountDDL.DataBind();
            }
        }

        public string Message
        {
            set { Label1.Text = value; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                Presenter.OnViewInitialized();
            Presenter.RefreshView();
        }

        protected void ObjectContainerDataSource1_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            var user = (DepartmentUser) e.Instance;
            try
            {
                Presenter.OnDeleteDepartmentUser(user);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.RefreshView();
            }
        }

        protected void ObjectContainerDataSource1_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            var user = (DepartmentUser) e.Instance;
            try
            {
                Presenter.OnUpdateDepartmentUser(user);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.RefreshView();
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            try
            {
                Presenter.OnCreateDepartmentUser(AccountDDL.SelectedItem.Value, RoleDDL.SelectedItem.Value);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.RefreshView();
            }
        }
    }
}